package cn.laike.modules.dev.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import cn.laike.common.utils.PageUtils;
import cn.laike.common.utils.R;
import cn.laike.common.validator.ValidatorUtils;
import cn.laike.modules.dev.entity.MemberEntity;
import cn.laike.modules.dev.service.MemberService;
import cn.laike.modules.dev.service.OrderService;



/**
 * 会员信息
 *
 * @author ziye
 * @email 524748907@qq.com
 * @date 2019-02-20 16:02:11
 */
@RestController
@RequestMapping("dev/member")
public class MemberController {
    @Autowired
    private MemberService memberService;
    
    @Autowired
    private OrderService orderService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("dev:member:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = memberService.queryPage(params);
        List<MemberEntity> list = (List<MemberEntity>) page.getList();
        List<String> openids = list.stream().map(MemberEntity::getOpenid).collect(Collectors.toList());
        System.out.println(openids);
        List<Map> openidList = orderService.statOrderList(openids);
        for (MemberEntity member : list) {
        	member.setOrderCount(0);
			member.setOrderDays(0);
			for (Map map : openidList) {
				if(map.get("openid").equals(member.getOpenid())) {
					member.setOrderCount(map.get("count"));
					member.setOrderDays(map.get("days"));
					openidList.remove(map);
					break;
				}
			}
			
		}
        
       

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("dev:member:info")
    public R info(@PathVariable("id") Integer id){
        MemberEntity member = memberService.selectById(id);

        return R.ok().put("member", member);
    }
    
    
    @RequestMapping("/updateMemberType")
    public R updateMemberType(Integer id,String type){
    	MemberEntity member = memberService.selectById(id);
    	member.setType(type);
    	memberService.insertOrUpdate(member);
    	return R.ok("保存成功");
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("dev:member:save")
    public R save(@RequestBody MemberEntity member){
        memberService.insert(member);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("dev:member:update")
    public R update(@RequestBody MemberEntity member){
        ValidatorUtils.validateEntity(member);
        memberService.updateAllColumnById(member);//全部更新
        
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("dev:member:delete")
    public R delete(@RequestBody Integer[] ids){
        memberService.deleteBatchIds(Arrays.asList(ids));

        return R.ok();
    }

}
